package com.example.newland.application42;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nle.mylibrary.claimer.rfid.RFIDReadListener;
import com.nle.mylibrary.claimer.rfid.SingleEpcListener;
import com.nle.mylibrary.databus.DataBus;
import com.nle.mylibrary.databus.DataBusFactory;
import com.nle.mylibrary.databus.ReciveData;
import com.nle.mylibrary.device.RFID;

public class MainActivity extends AppCompatActivity {

    private EditText main_ed_tiaoma;
    private TextView main_tv_name;
    private TextView main_tv_price;
    private DataBus dataBus;
    private TextToSpeech tts;
    private RFID rfid;
    private SharedPreferences config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_ed_tiaoma = findViewById(R.id.main_ed_tiaoma);
        main_tv_name = findViewById(R.id.main_tv_name);
        main_tv_price = findViewById(R.id.main_tv_price);


        config = getSharedPreferences("config", MODE_PRIVATE);
        SharedPreferences.Editor edit = config.edit();
        edit.putString("E2 80 69 95 00 00 40 12 6C C1 6C C9","华为 mate 20,5999元");
        edit.putString("E2 80 69 95 00 00 40 12 6C C1 48 C9","IPhoneXS,2299元");
        edit.putString("E2 80 69 95 00 00 50 12 6C C2 40 C9","小米Mix3,7699元");
        edit.commit();

        dataBus = DataBusFactory.newSocketDataBus(Util.ip, Util.port);

        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {

            }
        });

        dataBus.setReciveDataListener(new ReciveData() {
            @Override
            public String getReciveData(byte[] bytes) {
                return null;
            }
        });
        rfid = new RFID(dataBus,null);

        new Thread(){
            @Override
            public void run() {
                super.run();
                while(true){

                    try {
                        rfid.readSingleEpc(new SingleEpcListener() {
                            @Override
                            public void onVal(String s) {
                                Log.i("卡号",s);
                                if(s != null){
                                    String configString = config.getString(s, "");
                                    if(configString != null && !configString.equals("")){
                                        String[] split = configString.split(",");
                                        Message message = new Message();
                                        message.what = 1;
                                        Bundle bundle = new Bundle();
                                        bundle.putString("s",s);
                                        bundle.putString("name",split[0]);
                                        bundle.putString("jiage",split[1]);
                                        tts.speak(configString,TextToSpeech.QUEUE_FLUSH,null,null);
                                        message.setData(bundle);
                                        handler.sendMessage(message);
                                    }

                                }
                            }

                            @Override
                            public void onFail(Exception e) {
                                Log.e("err",e.getMessage() == null?"":e.getMessage());
                            }
                        });

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }.start();

    }

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 1:
                    Bundle data = msg.getData();
                    String name = data.getString("name");
                    String jiage = data.getString("jiage");
                    String s = data.getString("s");
                    main_tv_name.setText(name);
                    main_tv_price.setText(jiage);
                    main_ed_tiaoma.setText(s);
                    break;
            }
        }
    };

}